using System;
using UnityEngine;
using UnityEngine.Serialization;

namespace Script.UI.Panel
{
    public class AutoController : MonoSingle<AutoController>
    {
        public bool isStart;

        [FormerlySerializedAs("nowID")] public int nowIndex;
        public AutoControlConfig nowConfig;

        public float tick = 0;

        public void Awake()
        {
            AutoControlConfig.Init();
        }

        public void SetIsStart(bool isStart)
        {
            InitOnStart();
            this.isStart = isStart;
        }

        public void InitOnStart()
        {
            nowIndex = 0;
            nowConfig = AutoControlConfig.Confs[nowIndex];
        }

        public void Update()
        {
            if (isStart == false)
            {
                return;
            }
            if (nowConfig == null)
            {
                return;
            }
            tick += Time.deltaTime;
            if (nowConfig.time < tick)
            {
                tick = 0;
                ChangeNextId();
            }
        }

        public void ChangeNextId()
        {
            nowIndex = (nowIndex + 1) % AutoControlConfig.Confs.Count;
            nowConfig = AutoControlConfig.Confs[nowIndex];
            UI_MainWnd.Ins.SetCurrentLocation(nowConfig.id);
            // GlobalEvent.DispatchEvent(EventName.OnLocationClicked,nowConfig.id);
        }
    }
}